package com.mmc.controller;


import com.github.wxpay.sdk.WXPayUtil;
import com.hzit.common.utils.HttpClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;


import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 作者：李嘉浩
 * 时间：2024-10-22-9:34
 * 功能：
 */

@RestController
public class WxPayController {

    @Value("${weixin.appid}")
    private String appid;
    @Value("${weixin.partner}")
    private String partner;
    @Value("${weixin.partnerkey}")
    private String partnerkey;
    @Value("${weixin.notifyurl}")
    private String notifyurl;

    @GetMapping("pay")
    public Map<String,String> unifiedorder(String body, String outTradeNo , Integer totalFee) {
        try {
            HttpClient httpClient = new HttpClient("https://api.mch.weixin.qq.com/pay/unifiedorder");
            httpClient.setHttps(true);

            HashMap<String, String> map = new HashMap<>();

            // 设置支付特有的参数
            map.put("body", body);
            map.put("out_trade_no", outTradeNo);
            map.put("total_fee", totalFee + "");

            // 设置随机字符串
            map.put("nonce_str", WXPayUtil.generateNonceStr());

            // 设置公共参数
            map.put("appid", appid);
            // 设置商户号id
            map.put("mch_id", partner);
            // 设置回调地址
            map.put("notify_url", notifyurl);
            map.put("spbill_create_ip", "127.0.0.1");
            // 支付类型
            map.put("trade_type", "NATIVE");

            // 设置秘钥
            String s = WXPayUtil.generateSignedXml(map, partnerkey);

            // 设置参数
            httpClient.setXmlParam(s);


            httpClient.post();

            String content = httpClient.getContent();

            return WXPayUtil.xmlToMap(content);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    @GetMapping("search")
    public Map<String,String> search( String outTradeNo) {
        try {
            HttpClient httpClient = new HttpClient("https://api.mch.weixin.qq.com/pay/orderquery");
            httpClient.setHttps(true);

            HashMap<String, String> map = new HashMap<>();

            // 设置支付特有的参数
            map.put("out_trade_no", outTradeNo);


            // 设置随机字符串
            map.put("nonce_str", WXPayUtil.generateNonceStr());

            // 设置公共参数
            map.put("appid", appid);
            // 设置商户号id
            map.put("mch_id", partner);

            // 设置秘钥
            String s = WXPayUtil.generateSignedXml(map, partnerkey);

            // 设置参数
            httpClient.setXmlParam(s);


            httpClient.post();

            String content = httpClient.getContent();

            return WXPayUtil.xmlToMap(content);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }


    @GetMapping("refund")
    public Map<String,String> refund( String outTradeNo, Integer totalFee,String outRefundNo) {
        try {
            HttpClient httpClient = new HttpClient("https://api.mch.weixin.qq.com/secapi/pay/refund");
            httpClient.setHttps(true);

            HashMap<String, String> map = new HashMap<>();

            // 设置支付特有的参数
            map.put("out_trade_no", outTradeNo);
            map.put("total_fee", totalFee + "");
            map.put("refund_fee", totalFee + "");
            map.put("out_refund_no", outRefundNo);

            // 设置随机字符串
            map.put("nonce_str", WXPayUtil.generateNonceStr());

            // 设置公共参数
            map.put("appid", appid);
            // 设置商户号id
            map.put("mch_id", partner);

            // 设置秘钥
            String s = WXPayUtil.generateSignedXml(map, partnerkey);

            // 设置参数
            httpClient.setXmlParam(s);


            httpClient.post();

            String content = httpClient.getContent();

            System.out.println(content);
            return WXPayUtil.xmlToMap(content);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }
}
